<template>
  <el-form
    ref="ruleFormRef"
    label-position="left"
    :model="addUserFrom"
    status-icon
    :rules="rules"
    label-width="70px"
    class="demo-ruleForm"
  >
    <el-form-item prop="username" label="输入昵称">
      <el-input
        v-model="addUserFrom.username"
        placeholder="请输入昵称"
        :prefix-icon="UserFilled"
      />
    </el-form-item>

    <el-form-item label="输入密码" prop="password">
      <el-input
        v-model="addUserFrom.password"
        type="password"
        placeholder="请输入密码"
        autocomplete="off"
        :prefix-icon="Lock"
      />
    </el-form-item>

    <el-form-item label="确认密码" prop="checkPassword">
      <el-input
        v-model="addUserFrom.checkPassword"
        type="password"
        placeholder="请确认密码"
        autocomplete="off"
        :prefix-icon="Lock"
      />
    </el-form-item>
    <el-form-item label="输入邮箱" prop="email">
      <el-input
        v-model="addUserFrom.email"
        type="email"
        placeholder="请确认邮箱"
        autocomplete="off"
        :prefix-icon="Message"
      />
    </el-form-item>
    <el-form-item label="验 证 码" prop="code">
      <el-input
        v-model="addUserFrom.code"
        placeholder="请输入验证码"
        autocomplete="off"
        :prefix-icon="Key"
      />
    </el-form-item>

    <el-form-item>
      <SendCode :email="addUserFrom.email"></SendCode>
    </el-form-item>

    <el-form-item>
      <div class="submitBtn">
        <el-button @click="submitForm(ruleFormRef)">确认</el-button>
        <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
      </div>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { ref, reactive, defineAsyncComponent, inject } from 'vue'
import { Lock, UserFilled, Message, Key } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
// 验证
import {
  validatePass,
  validateUsername,
  validateEmail,
  validateCode,
} from '@/hooks/login/validate'

import { ElMessage } from 'element-plus'
// 导入函数

import { register } from '@/utils/http/login'
const SendCode = defineAsyncComponent(
  () => import('@/components/login/SendCode.vue')
)
const ruleFormRef = ref<FormInstance>()

const addUserFrom = reactive({
  username: '',
  password: '',
  checkPassword: '',
  email: '',
  code: '',
})
// 二次修改密码
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请确认密码'))
  } else if (value !== addUserFrom.password) {
    callback(new Error('两次密码不一致!'))
  } else {
    callback()
  }
}
const rules = reactive({
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePass, trigger: 'blur' }],
  checkPassword: [{ validator: validatePass2, trigger: 'blur' }],
  email: [{ validator: validateEmail, trigger: 'blur' }],
  code: [{ validator: validateCode, trigger: 'blur' }],
})

// 清空表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
const store: any = inject('store')
// 提交
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      let res = await register(
        addUserFrom.email,
        addUserFrom.code,
        addUserFrom.username,
        addUserFrom.password
      )
      res.data.ok
        ? ElMessage.success(`注册成功，欢迎${res.data.userBase.username}`)
        : ElMessage.error(res.data.info)

      res.data.ok && store.SetUserBase(res.data.userBase)
      res.data.ok && formEl.resetFields()
    } else {
      return false
    }
  })
}
</script>
<style lang="scss" scoped>
@media (max-width: 760px) {
  .demo-ruleForm {
    :deep(.el-form-item__label) {
      color: #fff;
    }
  }
}
</style>
